<template>
  <div>
    <div class="cashier-dz">
      <dl class="cashier-dz-add" v-if="settleData.delivery_addresses && settleData.delivery_addresses.length <= 0">
        <a href="/pages/address/main?type=cashier">
          <i class="iconfont icon-tianjia"></i>添加收货地址
        </a>
      </dl>
      <dl class="user-address" v-else @click="toAddressList">
        <dt>
          <i class="iconfont icon-dizhi"></i>
        </dt>
        <dd>
          <p>收货人：{{settleData.delivery_addresses[0].consignee_name}}
            <span class="fr">{{settleData.delivery_addresses[0].consignee_phone}}</span>
          </p>
          <p class="ellipsis">{{settleData.delivery_addresses[0].address}}</p>
          <p class="tip">请填写真实收件人姓名，否则可能造成物流延迟或海关退回的情况</p>
        </dd>
      </dl>
    </div>

    <div class="cashier-sp">
      <dl class="cashier-dl" v-for="(product,index) in settleData.products" :key="index">
        <dt>
          <img :src="product.cover+'?x-oss-process=image/resize,m_pad,h_400,w_400,color_ffffff/quality,Q_80/format,jpg'">
        </dt>
        <dd>
          <h2>{{product.designer_name}}</h2>
          <p>{{product.product_name}}</p>
          <p class="sp-bji">尺码 ：{{product.size}}</p>
        </dd>
        <h3>
          <font>x{{product.product_qty}}</font>{{product.currency_logo}}{{product.show_price}}({{product.calculation_price_text}})
        </h3>
      </dl>
    </div>

    <div class="cashier-ps" @click="toShipping">
      <span style="margin-top: 7px; float: left">配送说明</span>
      <p id="ps-out-info">{{defaultshippingId.shipping_title}}
        <font>{{defaultshippingId.currency_logo}}{{defaultshippingId.freight}}</font>
      </p>
      <i class="iconfont icon-xiayige cIconf"></i>
    </div>

    <div class="cashier-shrink" @click="handleReplace">
      <span>退换须知</span>
      <i class="iconfont cIconf" :class="{'icon-shouqi':replaceshow,'icon-zhankai':!replaceshow}"></i>
    </div>

    <div class="shrink-text" v-show="replaceshow">
      下列情况，7天内可以申请退换服务：<br> 1.实际收到商品发生破损，缺失等质量问题，严重影响使用
      <br> 2.包裹丢失
      <br> 3.实际收到的商品与订单信息不相符
      <br> 如有以上问题可与客服联系，进行退换。
    </div>

    <div class="benefit-selectbox" @click="toCouponsPage">
      <div class="benefit-select"><b>优惠券</b>
        <font v-if="settleData.default_coupon">- ¥{{settleData.default_coupon.amount_yuan}}</font>
        <font v-else>无可用优惠券</font>
        <i class="iconfont icon-xiayige cIconf"></i>
      </div>
    </div>

    <div class="cashier-pay">
      <h2>支付方式</h2>
      <dl>
        <dt class="pay-ico"><i class="iconfont icon-weixin1"></i></dt>
        <dd>微信支付<br>方便快捷，可支持银行卡支付</dd>
      </dl>
    </div>
    <div class="cashier-je">
      <ul>
        <li>商品金额： <span> ¥{{settleData.prices.totalProductPrice}}({{settleData.prices.currency_logo}}{{settleData.prices.totalCurrencyShowPrice}})</span></li>
        <li>汇率： <span>{{settleData.prices.exchangeRate}}</span></li>
        <li>运费 <span> ¥{{settleData.prices.deliveryFee}}</span></li>
        <li v-if="settleData.prices.couponAmount != 0">优惠券 <span>- ¥{{settleData.prices.couponAmount}}</span></li>
      </ul>
    </div>

    <div class="cashier-bottom-row">
      <input v-model="userRemark" type="text" placeholder="请备注您的要求，方便为您提供更好的服务">
    </div>

    <div class="posiHeight"></div>

    <div class="cashier-bottom-bar">
      <span class="shoppingcart-bar-left" id="total-price-dom">订单总价：¥<font class="futuraFont">{{settleData.prices.amoutPayable}}</font></span>
      <button @click="settlementBtn">结算</button>
    </div>
  </div>
</template>

<script>
import wxpayfn from '@/utils/paymaneger'
import wechat from '@/utils/wx'
import {mapState, mapActions} from 'vuex'
export default {
  data() {
    return {
      deliveryAddresses:'',
      replaceshow: false,
      userRemark:''
    }
  },
  computed: {
    // 计算属性的 getter
    ...mapState('cart', ['settleData','productsObj','defaultshippingId']),
  },
  onLoad() {
    this.$wx.setTitleBarTitle('收银台');
  },
  methods: {
    ...mapActions('cart', [
        'getCarts'
      ]),
    settlementBtn(){
      var postParams = {
        amount_payable: this.settleData.prices.amoutPayable,
        order_details: this.productsObj,
        shipping_id: this.defaultshippingId.shipping_id
      }

      if(this.settleData.delivery_addresses.length > 0) {
          postParams.consignee = this.settleData.delivery_addresses[0].consignee_name,
          postParams.consignee_address = this.settleData.delivery_addresses[0].address,
          postParams.consignee_telephone = this.settleData.delivery_addresses[0].consignee_phone
          postParams.address_id = this.settleData.delivery_addresses[0].address_id
      } else{
          this.$wx.showShortToast("请选择收货地址")
          return false
      }

      if(this.userRemark){
        postParams.remark = this.userRemark;
      }

      if(this.settleData.default_coupon){
        postParams.coupon_id = this.settleData.default_coupon.coupon_id;
      }

      this.postCreate(postParams)
    },
    async postCreate(postParams) {
      const data = await this.$fly.post(this.$url.URL_CREATE, postParams )
      if (data.successCode === 1) {
         var orderNo = data.data.order_no;
         this.getCarts()
         wxpayfn.pay(orderNo,(data) => {
           if(data){
             this.$wx.showShortToast('支付成功')
             this.$wx.jumper('/pages/orderList/main')
             this.$root.$eventHub.$emit('requestPayBack', '')
           }else{
             this.$wx.showShortToast('支付失败')
             this.$wx.jumper('/pages/orderList/main')
             this.$root.$eventHub.$emit('requestPayBack', '')
           }
         })
      } else{
        this.$wx.showShortToast(data.message)
      }
    },
    handleReplace() {
      this.replaceshow = !this.replaceshow
    },
    toShipping(){
      this.$wx.jumper('/pages/cashierShipping/main')
    },
    toCouponsPage(){
      if(this.settleData.default_coupon){
        this.$wx.jumper('/pages/cashierCoupons/main')
      }
    },
    toAddressList(){
      this.$wx.jumper('/pages/addressList/main')
    }
  }
}
</script>

<style scoped>
.cashier-dz {
  background: #fff;
  margin-bottom: 10px;
}

.cashier-dz dl {
  padding: 2% 4%;
  overflow: hidden;
  position: relative;
}

.cashier-dz dl dt {
  position: absolute;
  top: 50%;
  margin-top: -9px;
  left: 14px;
  display: inline-block;
}

.cashier-dz dl dd {
  float: left;
  width: 92%;
  margin-left: 8%;
  line-height: 1.6em;
}

.cashier-dz dl dd p span {
  margin-left: 12px;
  margin-right: 16px;
}

.cashier-dz dl dd p.tip {
  color: #ff4545;
  font-size: 0.9em;
  line-height: 1em;
}

.cashier-dz dl dd p.ellipsis {
  color: #666;
}

.cashier-dz dl.cashier-dz-add {
  text-align: center;
}

.cashier-dz dl.cashier-dz-add a {
  padding: 2% 4%;
  display: block;
}

.cashier-dz dl.cashier-dz-add a i {
  display: inline-block;
}

.cashier-dz dl a.addrEdtBtn {
  position: absolute;
  top: 10px;
  right: 8px;
}

.address-content {
  padding: 4%;
}

.cashier-dz ul {
  padding: 2%;
  overflow: hidden;
  position: relative;
}

.cashier-dz ul li {
  line-height: 22px;
}

.cashier-sp {
  margin-top: 10px;
  position: relative;
  background: #fff;
}

.discount-ico {
  position: absolute;
  top: 0;
  right: 0;
  width: 41px;
  height: 39px;
}

.cashier-dl {
  padding: 2% 4%;
  overflow: hidden;
  position: relative;
}

.cashier-dl dt {
  float: left;
}

.cashier-dl dt img {
  width: 66px;
  height: 66px;
}

.cashier-dl dd {
  float: left;
  margin-left: 4%;
  line-height: 1.2em;
  width: 48%;
}

.cashier-dl dd p,
.cashier-sp dl dd h2 {
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
}

.cashier-dl dd h2 {
  font-family: 'FuturaStd';
  line-height: 1.4em;
  font-size: 1.1em;
}

.cashier-dl h3 {
  text-align: right;
  position: absolute;
  right: 2%;
  top: 50%;
  -webkit-transform: translateY(-50%);
  transform: translateY(-50%);
  font-family: 'FuturaStd';
}

.cashier-dl h3 font {
  display: block;
  color: #6e6e6e;
}

.cashier-dl dd p.sp-bji {
  font-size: 0.8em;
  color: #999;
  line-height: 100%;
  margin-top: 10px;
}

.cashier-ps {
  background: #fff;
  padding: 10px 8% 10px 4%;
  margin-top: 10px;
  overflow: hidden;
  position: relative;
}

.cashier-ps span {
  line-height: 100%;
}

.cashier-ps p {
  float: right;
  font-size: 0.8em;
  text-align: right;
  line-height: 100%;
}

.cashier-ps p font {
  display: block;
  color: #999;
  margin-top: 4px;
  font-family: 'FuturaStd';
}

.cIconf {
  position: absolute;
  top: 50%;
  right: 8px;
  -webkit-transform: translateY(-50%);
  transform: translateY(-50%);
}

.cashier-shrink {
  padding: 10px 8% 10px 4%;
  overflow: hidden;
  position: relative;
}

.cashier-shrink dl {
  padding: 2% 4% 1%;
  overflow: hidden;
}

.cashier-shrink dl dd {
  float: left;
  font-size: 1.1em;
}

.cashier-shrink dl dt {
  float: right;
  height: 18px;
}

.shrink-text {
  padding: 0 4% 2%;
  line-height: 1.4em;
  font-size: 0.9em;
  color: #999;
}

.benefit-selectbox {
  padding: 10px 8% 10px 4%;
  background-color: #fff;
  position: relative;
}

.benefit-select {
  width: 100%;
  line-height: 20px;
}

.benefit-select b {
  font-weight: normal;
  display: inline-block;
}

.benefit-select font {
  float: right;
  color: #ff4545;
  font-size: 0.9em;
  font-family: 'FuturaStd';
}

.benefit-select span {
  float: right;
}

.cashier-pay {
  padding: 2% 4% 0;
  background-color: #fff;
  margin-top: 10px;
}

.cashier-pay h2 {
  line-height: 24px;
  font-size: 1.1em;
}

.pay-ico {
  width: 35px;
  height: 35px;
  line-height: 35px;
}

.pay-ico i {
  font-size: 32px;
  color: #11c019;
}

.cashier-pay dl {
  padding: 2% 0;
  border-bottom: 1px #f2f2f2 solid;
  overflow: hidden;
}

.cashier-pay dl:last-child {
  border: none;
}

.cashier-pay dl dt {
  float: left;
  margin-top: 3px;
}

.cashier-pay dl dd {
  float: left;
  padding-left: 12px;
  color: #8a8a8a;
  line-height: 1.1em;
  padding-top: 4px;
}

.cashier-je {
  padding: 3% 4%;
  margin-top: 6px;
  background: #fff;
}

.cashier-je ul li {
  line-height: 1.8em;
}

.cashier-je ul li span {
  float: right;
  font-family: 'FuturaStd';
}

.cashier-bottom-row {
  height: 40px;
  margin-top: 6px;
}

.posiHeight{height: 55px;}

.cashier-bottom-row input {
  padding: 0 8px;
  width: 100%;
  height: 40px;
  line-height: 40px;
  background: #fff;
  box-sizing: border-box;
}
</style>
